const cloudHelper = require('@/utils/helper/cloud_helper.js');

// 验证图片URL是否有效
function isValidImageUrl(url) {
    if (!url) return false;
    // 检查是否是有效的URL格式
    if (url.startsWith('http://') || url.startsWith('https://') || url.startsWith('data:image/') || url.startsWith('/')) {
        return true;
    }
    // 检查是否是云存储路径
    if (url.startsWith('cloud://')) {
        return true;
    }
    return false;
}

async function config1({
    cover,
    title,
    desc,
    qr,
    bg = '',
    user = '',
    avatar = '' //头像
}) {
    console.log('海报配置参数:', { cover, title, desc, qr, bg, user, avatar });
    
    // 处理云存储图片
    if (cover && cover.startsWith('cloud')) {
        cover = await cloudHelper.getTempFileURLOne(cover);
    }
    if (qr && qr.startsWith('cloud')) {
        qr = await cloudHelper.getTempFileURLOne(qr);
    }
    if (avatar && avatar.startsWith('cloud')) {
        avatar = await cloudHelper.getTempFileURLOne(avatar);
    }
    
    // 验证图片URL，使用默认图片
    if (cover && !isValidImageUrl(cover)) {
        console.warn('封面图片URL无效，使用默认图片:', cover);
        cover = '/static/images/share.jpg';
    } else if (!cover) {
        console.log('使用默认封面图片');
        cover = '/static/images/share.jpg';
    }
    
    if (qr && !isValidImageUrl(qr)) {
        console.warn('二维码图片URL无效，清空:', qr);
        qr = '';
    }
    if (avatar && !isValidImageUrl(avatar)) {
        console.warn('头像图片URL无效，清空:', avatar);
        avatar = '';
    }
    let posterConfig = {
        width: 480,
        // rpx
        height: 650,
        backgroundColor: '#eeeeee'
    };
    if (bg) {
        posterConfig.backgroundColor = bg;
    }
    let blocks = [];
    blocks = [
        {
            x: 30,
            y: 30,
            backgroundColor: '#ffffff',
            width: 420,
            height: 590,
            borderRadius: 20
        }
    ];
    let texts = [];
    texts = [
        {
            x: 50,
            y: 350,
            text: title,
            width: 360,
            lineNum: 2,
            lineHeight: 40,
            fontSize: 26,
            color: '#000000',
            textAlign: 'left',
            zIndex: 9999
        },
        {
            x: 55,
            y: 510,
            text: '长按识别小程序码',
            fontSize: 18,
            color: '#aaaaaa',
            zIndex: 9999
        },
        {
            x: 55,
            y: 540,
            text: desc,
            fontSize: 18,
            color: '#aaaaaa',
            zIndex: 9999
        }
    ];
    if (user) {
        texts.push({
            x: 55,
            y: 480,
            text: user,
            fontSize: 18,
            color: bg,
            zIndex: 9999
        });
    }
    let images = [];
    if (cover) {
        images.push({
            // 底图
            x: 40,
            y: 40,
            url: cover,
            width: 400,
            height: 260,
            zIndex: 999
        });
    }
    if (qr) {
        images.push({
            // 小程序码
            x: 310,
            y: 460,
            url: qr,
            width: 120,
            height: 120
        });
    }
    if (avatar) {
        images.push({
            // 头像
            x: 343,
            y: 493,
            url: avatar,
            width: 54,
            height: 54,
            borderRadius: 54
        });
    }
    posterConfig.texts = texts;
    posterConfig.blocks = blocks;
    posterConfig.images = images;
    return posterConfig;
}
module.exports = {
    config1
};
